package nsq

import (
	"context"
	"fmt"
	"time"

	"my-zero-tiktok/apps/api/internal/svc"
	"my-zero-tiktok/apps/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type NSQPushLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewNSQPushLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NSQPushLogic {
	return &NSQPushLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *NSQPushLogic) NSQPush(req *types.NSQPush) (resp *types.Empty, err error) {
	go func() {
		for i := 0; i < 3; i++ {
			msg := fmt.Sprintf("%s ==> %d", req.Message, i)
			err = l.svcCtx.NSQProducer.Publish(l.svcCtx.Config.NSQ.Topic, []byte(msg))
			if err != nil {
				l.Logger.Errorf("[NSQPushLogic] err: %v", err)
			}
		}
	}()
	time.Sleep(1)

	//l.svcCtx.NSQConsumer.AddHandler(&Handler{})
	//go func() {
	//	err = l.svcCtx.NSQConsumer.ConnectToNSQD(l.svcCtx.Config.NSQ.URL)
	//	if err != nil {
	//		l.Logger.Errorf("[NSQConsumer] err: %v", err)
	//	}
	//}()

	return &types.Empty{}, nil
}
